package com.titaniume.rabbitmq.springbootrabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * @ProjectName: rabbitmq
 * @Package: com.titaniume.rabbitmq.springbootrabbitmq.config
 * @ClassName: DelayedQueueConfig
 * @Author: titan
 * @Description: 插件实现延迟
 * @Date: 2021/12/31 21:51
 * @Version: 1.0
 */
@Configuration
public class DelayedQueueConfig {

    //队列
    public static final String DELAYPED_QUEUE_NAME ="dalayed.queue";
    //交换机
    public static final String DELAYPED_EXCHANGE_NAME ="dalayed.exchange";
    //routingKey
    public static final String DELAYPED_ROUTING_KEY ="dalayed.routingkey";

    @Bean
    public Queue delayedQueue(){
        return  new Queue(DELAYPED_QUEUE_NAME);
    }

    //声明交换机 基于插件
    @Bean
    public CustomExchange delayedExchange(){
        HashMap<String, Object> arguments = new HashMap<>();
        //自定义交换机的类型
        arguments.put("x-delayed-type","direct");

        /**
         * 1.交换机名称
         * 2.交换机类型
         * 3.是否需要持久化
         * 4.是否需要自动删除
         * 5.其他参数
         */
        return new CustomExchange(DELAYPED_EXCHANGE_NAME,"x-delayed-message",true,false,arguments);
    }


    //绑定
    @Bean
    public Binding delayedQueueBindingDelayedExchange(@Qualifier("delayedQueue") Queue delayedQueue,
                                       @Qualifier("delayedExchange") CustomExchange delayedExchange){
        return BindingBuilder.bind(delayedQueue).to(delayedExchange).with(DELAYPED_ROUTING_KEY).noargs();
    }
}
